﻿<?php
/**
 * @package Joomla
 * @subpackage Jmonoslideshow
 * @copyright (C) 2010 - Matthieu BARBE - www.ccomca.com
 * @license GNU/GPL v2
 * 
 * Jmonoslideshow is a Joomla extension for Monoslideshow.
 *
 * Jmonoslideshow uses :
 * Monoslideshow (http://www.monoslideshow.com/)
 * Plupload (http://www.plupload.com/) by Moxicode Systems
 * json class by Michal Migurski, Matt Knapp, Brett Stimmerman
 * jquery a JavaScript Library (http://jquery.com/)
 * getid3 library (http://getid3.sourceforge.net/)
 * swfobject is an easy-to-use and standards-friendly method to embed Flash content, which utilizes one small JavaScript file (http://code.google.com/p/swfobject/)
 * Chained Selects jQuery Plugin (http://www.appelsiini.net/2010/jquery-chained-selects)
 *
 * Jmonoslideshow is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
require_once JPATH_COMPONENT_ADMINISTRATOR.DS.'library'.DS.'ThumbLib.inc.php';
$doc = &JFactory::getDocument();
$params = &JComponentHelper::getParams( 'com_jmonoslideshow' );
$imagewidth = $params->get( 'width_big_image', "" );
$imageheight = $params->get( 'height_big_image', "" );
$maxThumbnailWidth = $params->get('width_vignette', '120');
$maxThumbnailHeight = $params->get( 'height_vignette', '90' );
$base_upload_dir = JPATH_ROOT.DS . 'media' . DS . 'com_jmonoslideshow' . DS . 'upload' ;
$jpgQuality = $params->get( 'qualtiy_image', '75' );
$options = array('jpegQuality' => $jpgQuality);

if ($imagewidth == "" || $imagewidth == 0) $imagewidth = MediaHelper::getSizeImageLimit(); else $imagewidth = $imagewidth;
if ($imageheight == "" || $imageheight == 0) $imageheight = MediaHelper::getSizeImageLimit(); else $imageheight = $imageheight;

//load css
JHTML::stylesheet( 'jmonoslideshow.css', JMONOSLIDESHOW_MEDIA_CSS_ADMIN );
JHTML::stylesheet( 'jquery-ui.custom.css', JMONOSLIDESHOW_MEDIA_CSS_ADMIN.'Aristo/' );
JHTML::stylesheet( 'jquery.tagsinput.css', JMONOSLIDESHOW_MEDIA_CSS_ADMIN );
JHTML::stylesheet( 'jquery.qtip.css', JMONOSLIDESHOW_MEDIA_CSS_ADMIN );
//load jquery
$doc->addScript(JMONOSLIDESHOW_MEDIA_JS_ADMIN.'jquery.min.js?version='.JMONOSLIDESHOW_JQUERY);
if (!$this->model->id || ($this->model->id && $this->model->type != 3))
	{
	//load plupload with flash runtime
	$doc->addScript(JMONOSLIDESHOW_MEDIA_JS_PLUPLOAD_ADMIN.'plupload.min.js?version='.JMONOSLIDESHOW_PLUPLOAD);
	$doc->addScript(JMONOSLIDESHOW_MEDIA_JS_PLUPLOAD_ADMIN.'plupload.flash.min.js?version='.JMONOSLIDESHOW_PLUPLOAD);
	$doc->addScript(JMONOSLIDESHOW_MEDIA_JS_PLUPLOAD_ADMIN.'jquery.plupload.queue.min.js?version='.JMONOSLIDESHOW_PLUPLOAD);
	}
//load tag input
$doc->addScript(JMONOSLIDESHOW_MEDIA_JS_ADMIN.'jquery.tagsinput.min.js');
//load jquery ui
$doc->addScript(JMONOSLIDESHOW_MEDIA_JS_ADMIN.'jquery-ui.custom.min.js?version='.JMONOSLIDESHOW_JQUERYUI);
if ($params->get( 'tabsCookie', 1 ) == 1) $doc->addScript(JMONOSLIDESHOW_MEDIA_JS_ADMIN.'jquery.cookie.js?version='.JMONOSLIDESHOW_JQUERYUI);
//load qtips
$doc->addScript(JMONOSLIDESHOW_MEDIA_JS_ADMIN.'jquery.qtip.min.js');


$db =& JFactory::getDBO();
$nullDate = $db->getNullDate();

jimport('joomla.environment.uri' );
$host = JURI::root();

if (!$this->model->id || ($this->model->id && $this->model->type != 3))
	{
	$ajaxUpload = "
			jQuery.noConflict();
			jQuery(function() {
			jQuery( '#mediarss' ).hide();
					jQuery('#tags').tagsInput({
					width: 'auto',
					'defaultText':'Ajouter un tag',
					autocomplete_url:'".$host."administrator/index.php?option=com_jmonoslideshow&task=tags.searchTags&format=json'
				});		
			
		var uploader = new plupload.Uploader({
			runtimes : 'flash',
			multi_selection: false,
			browse_button : 'pickfiles',
			multipart :true,
			 multipart_params : {check : '".JUtility::getToken()."'},
			max_file_size : '".$params->get( 'upload_maxsize', 2 )."mb',
			chunk_size : '1mb',
			url : '".$host."administrator/index.php?option=com_jmonoslideshow&task=upload.uploadMedia&format=json',
			resize : {width : ".$imagewidth.", height : ".$imageheight.", quality : ".$params->get( 'qualtiy_image', 75 )."},
			flash_swf_url : '".JMONOSLIDESHOW_MEDIA_JS_PLUPLOAD_ADMIN."/plupload.flash.swf',
			filters : [
				{title : \"Media\", extensions : \"".MediaHelper::extension_authorize()."\"}
				
			]
		});
		
		
		
		
	
		uploader.bind('FilesAdded', function(up, files) {
			jQuery.each(files, function(i, file) {
				jQuery('#dialog-message').append(
					'<div id=\"' + file.id + '\">' +
					file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
				'</div>');
			});
		});
		
		
	
		uploader.bind('UploadProgress', function(up, file) {
			jQuery('#' + file.id + \" b\").html(file.percent + \"%\");
			jQuery( '#progressbar' ).progressbar({ value: file.percent });
		});
		
		uploader.bind('FileUploaded', function(up, file, json) {
				var obj = eval('(' + json.response + ')');
				jQuery('#upload_etat').val('ok');
				jQuery('#upload_media_name').val (obj.fileName);
				jQuery('#type').val (obj.type);
				jQuery('#extension').val (obj.extension);
				if (obj.html == 'success')
				{
				
					jQuery('#message_upload').removeClass('alert');
					jQuery('#message_upload').removeClass('info');
					jQuery('#message_upload').addClass('approved').html(obj.msg);
					jQuery( '#dialog-message' ).dialog('close' );
				}
				else
				{
					jQuery('#upload_etat').val('error');
					jQuery('#message_upload').removeClass('approved');
					jQuery('#message_upload').removeClass('info');
					jQuery('#message_upload').addClass('alert').html(obj.msg);	
				}
				jQuery('#pickfiles').html(\"  ". JText::_('COM_JMONOSLIDESHOW_UPLOAD_MEDIA' )." \");
		});
	
		jQuery('#uploadfiles').click(function(e) {
			
			uploader.start();
			e.preventDefault();
		});
		
		uploader.bind('Error', function(up, err) {
			 jQuery('#filelist').append(\"<div>Error: \" + err.code +
					 \", Message: \" + err.message +
					 (err.file ? \", File: \" + err.file.name : \"\") +
					 \"</div>\"
					 );
				 });
	
		uploader.init();
		
		jQuery('#message_upload').addClass('info').html(\"". JText::_( 'COM_JMONOSLIDESHOW_INFO_UPLOAD' )."\");
		
		uploader.bind('FilesAdded', function(up, files) {
			jQuery('#upload_etat').val('upload');
			jQuery('#message_upload').removeClass('alert');
			jQuery('#message_upload').removeClass('approved');
			jQuery('#message_upload').addClass('info').html(\"". JText::_( 'COM_JMONOSLIDESHOW_INFO_UPLOAD' )."\");
			jQuery('#pickfiles').html(\" <img src='".JMONOSLIDESHOW_MEDIA_IMAGE_ADMIN."/ajax-loader.gif' align='absmiddle' /> ". JText::_('COM_JMONOSLIDESHOW_UPLOADING_MEDIA' )." \");
			jQuery( '#dialog-message' ).dialog({
			modal: true,
			buttons: {
				Ok: function() {
					jQuery( this ).dialog( 'close' );
				}
			}
		});
			
			uploader.start();
			});
		
		});
			
			
			";
									 
										 
	$doc->addScriptDeclaration($ajaxUpload);
	
	if ($this->model->id)
		{
			$ajaxUploadThumb = "
					
					jQuery(function() {
			var uploaderthumb = new plupload.Uploader({
					runtimes : 'flash',
					multi_selection: false,
					browse_button : 'addThumb',
					multipart :true,
					 multipart_params : {check : '".JUtility::getToken()."'},
					max_file_size : '".$params->get( 'upload_maxsize', 2 )."mb',
					chunk_size : '1mb',
					url : '".$host."administrator/index.php?option=com_jmonoslideshow&task=upload.uploadThumb&format=json',
					resize : {width : ".$imagewidth.", height : ".$imageheight.", quality : ".$params->get( 'qualtiy_image', 75 )."},
					flash_swf_url : '".JMONOSLIDESHOW_MEDIA_JS_PLUPLOAD_ADMIN."/plupload.flash.swf',
					filters : [
						{title : \"Images\", extensions : \"".MediaHelper::extension_authorize()."\"}
						
					]
				});
				
				
				
				
			
				uploaderthumb.bind('FilesAdded', function(up, files) {
					jQuery.each(files, function(i, file) {
						jQuery('#dialog-message-thumb').append(
							'<div id=\"' + file.id + '\">' +
							file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
						'</div>');
					});
				});
				
				
			
				uploaderthumb.bind('UploadProgress', function(up, file) {
					jQuery('#' + file.id + \" b\").html(file.percent + \"%\");
					jQuery( '#progressbarthumb' ).progressbar({ value: file.percent });
				});
				
				uploaderthumb.bind('FileUploaded', function(up, file, json) {
						var obj = eval('(' + json.response + ')');
						jQuery('#upload_etat_thumb').val('ok');
						jQuery('#upload_thumb_name').val (obj.fileName);
						
						if (obj.html == 'success')
						{
							jQuery('#dialog-message-thumb').dialog('close' );
							jQuery('#message_upload').removeClass('alert');
							jQuery('#message_upload').removeClass('info');
							jQuery('#message_upload').addClass('approved').html(obj.msg);
						}
						else
						{
							jQuery('#upload_etat_thumb').val('error');
							jQuery('#message_upload').removeClass('approved');
							jQuery('#message_upload').removeClass('info');
							jQuery('#message_upload').addClass('alert').html(obj.msg);	
						}
						jQuery('#pickfiles').html(\"  ". JText::_('COM_JMONOSLIDESHOW_UPLOAD_MEDIA' )." \");
				});
			
				jQuery('#addThumb').click(function(e) {
					
					uploaderthumb.start();
					e.preventDefault();
				});
				
				uploaderthumb.bind('Error', function(up, err) {
					 jQuery('#dialog-message-thumb').append(\"<div>Error: \" + err.code +
							 \", Message: \" + err.message +
							 (err.file ? \", File: \" + err.file.name : \"\") +
							 \"</div>\"
							 );
						 });
			
				uploaderthumb.init();
				
				jQuery('#message_upload').addClass('info').html(\"". JText::_( 'COM_JMONOSLIDESHOW_INFO_UPLOAD' )."\");
				
				uploaderthumb.bind('FilesAdded', function(up, files) {
					jQuery('#upload_etat_thumb').val('upload');
					jQuery('#message_upload').removeClass('alert');
					jQuery('#message_upload').removeClass('approved');
					jQuery('#message_upload').addClass('info').html(\"". JText::_( 'COM_JMONOSLIDESHOW_INFO_UPLOAD' )."\");
					jQuery( '#dialog-message-thumb' ).dialog({
					modal: true,
					buttons: {
						Ok: function() {
							jQuery( this ).dialog( 'close' );
						}
					}
				});
					uploaderthumb.start();
					});
				
				
			});
					";
		
			$doc->addScriptDeclaration($ajaxUploadThumb);
		}
	
	}
if ($params->get( 'tabsCookie', 1 ) == 1) $cookie = "{cookie: {expires: 1}}"; else $cookie = "";
$script='

jQuery.noConflict();
jQuery(function() {
	
	
	jQuery(".infomedia").qtip({
   content: {
      text: "'.JText::_( 'COM_JMONOSLIDESHOW_LOADING' ).'", 
      ajax: {
         url: "'.$host.'administrator/index.php?option=com_jmonoslideshow&task=media.getMediaIntormation&format=raw&id='.$this->model->id.'", 
         type: "GET"
      }
   },
	  position: {
      viewport: jQuery(window)
   },
   style: {
				classes: "ui-tooltip-wiki ui-tooltip-light ui-tooltip-shadow"
			}
});
	
	jQuery( "#tabsmedia" ).tabs('.$cookie.');
	jQuery( "#addThumb" ).button({
            icons: {
                primary: "ui-icon-pencil"
            }
        });
	jQuery( "#viewVideo" ).button({
            icons: {
                primary: "ui-icon-play"
            }
        }).click(function() {
					
					jQuery( "#dialog-view-video" ).dialog( "open" );
					return false;

				});;
	 
	jQuery("#jform_rss").change(function() {
	var mediaType = jQuery("#jform_rss").val();
	 if (mediaType != 1)

		 {

			 jQuery( "#media" ).show();
			 jQuery( "#mediarss" ).hide();
			 

		 }
		 else
		 {
			 jQuery( "#mediarss" ).show();
			 jQuery( "#media" ).hide();
		 }
	
	});
	
		
});

';
									 
$doc->addScriptDeclaration($script);
?>
<script type="text/javascript">
	Joomla.submitbutton = function(task)
	{
			if (task == 'media.cancel' ) {
            Joomla.submitform(task, document.getElementById('media-form'));
        	}
			else if (jQuery("#jform_catid").val() == "0" ){
				alert( "<?php echo JText::_( 'COM_JMONOSLIDESHOW_YOU_MUST_SELECT_A_CATEGORY' );  ?>" );
			}
			else if ((jQuery("#mediaid").val() != "" && jQuery("#oldFilename").val() == "" && jQuery("#jform_rss").val() == 0) ||  (jQuery("#mediaid").val() == "" && jQuery("#upload_media_name").val() == "" && jQuery("#jform_rss").val() == 0) ){
				alert( "<?php echo JText::_( 'COM_JMONOSLIDESHOW_YOU_MUST_SELECT_A_MEDIA' );  ?>" );
			}
			else if (jQuery("#upload_etat").val() == "upload"){
				alert( "<?php echo JText::_( 'COM_JMONOSLIDESHOW_PLEASE_WAIT_AN_UPLOAD' );  ?>" );
			}
			else if (jQuery("#upload_etat_thumb").val() == "upload"){
				alert( "<?php echo JText::_( 'COM_JMONOSLIDESHOW_PLEASE_WAIT_AN_THUMB' );  ?>" );
			}
			else if (jQuery("#upload_etat").val() == "error"){
				alert( "<?php echo JText::_( 'COM_JMONOSLIDESHOW_ERROR_WITH_UPLOAD_MEDIA' );  ?>" );
			}
			else if (jQuery("#upload_etat_thumb").val() == "error"){
				alert( "<?php echo JText::_( 'COM_JMONOSLIDESHOW_ERROR_WITH_UPLOAD_THUMB' );  ?>" );
			}
			else
			{
				Joomla.submitform(task, document.getElementById('media-form'));
			}
	}
</script>

<form action="<?php echo JRoute::_('index.php?option=com_jmonoslideshow&view=media&layout=edit&id='.(int) $this->model->id); ?>" method="post" name="media-form" id="media-form" >
  <div id="message_upload"></div>
  <div class="col100">
  <div id="tabsmedia">
  <ul>
    <li><a href="#mediafields"><?php echo JText::_( 'COM_JMONOSLIDESHOW_GLOBAL' ); ?></a></li>
    <?php if ($this->model->id) : ?>
    <li><a href="#mediaoptions"><?php echo JText :: _('COM_JMONOSLIDESHOW_PARAMETERS'); ?></a></li>
    <?php endif; ?>
    <li><a href="#tagsoptions"><?php echo JText :: _('COM_JMONOSLIDESHOW_TAGS'); ?></a></li>
    <li><a href="#rules"><?php echo JText :: _('COM_JMONOSLIDESHOW_RULES'); ?></a></li>
  </ul>
  <div id="mediafields">
  <fieldset class="adminform">
  <table width="100%">
    <tr>
      <td><ul class="adminformlist">
          <li><?php echo $this->form->getLabel('title'); ?> <?php echo $this->form->getInput('title'); ?></li>
          <li><?php echo $this->form->getLabel('description'); ?> <?php echo $this->form->getInput('description'); ?></li>
          <li><?php echo $this->form->getLabel('link'); ?> <?php echo $this->form->getInput('link'); ?></li>
          <li><?php echo $this->form->getLabel('catid'); ?> <?php echo $this->form->getInput('catid'); ?></li>
          <li><?php echo $this->form->getLabel('published'); ?> <?php echo $this->form->getInput('published'); ?></li>
          <li><?php echo $this->form->getLabel('access'); ?> <?php echo $this->form->getInput('access'); ?></li>
          <?php if (!$this->model->id) : ?>
          <li><?php echo $this->form->getLabel('rss'); ?> <?php echo $this->form->getInput('rss'); ?></li>
          <?php endif  ?>
        </ul>
        <?php if (!$this->model->id || ($this->model->id && $this->model->type != 3)) : ?>
        <div id="media">
          <label for="addmedia"> <?php echo JText::_( 'COM_JMONOSLIDESHOW_ADD_MEDIA' ); ?>: </label>
          <div class="wrapper" style="position:relative; clear:both;">
            <div id="pickfiles" class="button"><?php echo JText::_( 'COM_JMONOSLIDESHOW_UPLOAD_MEDIA' ) ?></div>
          </div>
          <div id="message_upload"></div>
          <div id="dialog-message">
          <div id="progressbar" ></div>
        </div>
          <input name="upload_etat" id="upload_etat" type="hidden" value="" />
          <input name="upload_media_name" id="upload_media_name" type="hidden" value="" />
          <input name="type" id="type" type="hidden" value="<?php echo $this->model->type;?>" />
          <input name="oldFilename" id="oldFilename" type="hidden" value="<?php echo $this->model->filename;?>" />
          <input name="extension" id="extension" type="hidden" value="<?php echo $this->model->extension;?>" />
        </div>
        <?php endif  ?>
        <?php if (!$this->model->id || ($this->model->id && $this->model->type == 3)) : ?>
        <div id="mediarss">
          <li><?php echo $this->form->getLabel('link_rss'); ?> <?php echo $this->form->getInput('link_rss'); ?></li>
        </div>
        <?php endif  ?></td>
      <td valign="top">
	  
	  <?php if ($this->model->id) : ?>
	  <?php echo MediaHelper::getThumb($this->model->filename, $this->model->thumbname)  ?>
   <br style="clear:both;"/><a href="javascript:;" id="addThumb"><?php echo JText::_( 'COM_JMONOSLIDESHOW_CUSTOM_THUMB' ) ?></a>
    <?php if ($this->model->type == 2) : ?>
    <br /><br />
    <?php 
	
	$info = MediaHelper::info_media ($this->model->type, $this->model->filename); //print_r ($info['meta']['onMetaData']['width']);
	$width = $info['meta']['onMetaData']['width'] + 30;
	$height = $info['meta']['onMetaData']['height'] + 30;
	
	$scriptVideo='

	jQuery(function() {
		jQuery( "#dialog-view-video" ).dialog({
			autoOpen: false,
			resizable: true,
			height:'.$height.',
			width:'.$width.',
			modal: true,
			buttons: {
				"Fermer": function() {
					jQuery( this ).dialog( "close" );
				}
			}
		
		});		
	});
	';
	$doc->addScriptDeclaration($scriptVideo);
	?>
    <a href="javascript:;" id="viewVideo"><?php echo JText::_( 'COM_JMONOSLIDESHOW_VIEW_VIDEO' ) ?></a>
    <div id="dialog-view-video">
    <object type="application/x-shockwave-flash" data="<?php echo JMONOSLIDESHOW_MEDIA_JS_ADMIN; ?>dewtube.swf" width="<?php echo $info['meta']['onMetaData']['width'] ?>" height="<?php echo $info['meta']['onMetaData']['height'] ?>" id="dewtube">
<param name="allowFullScreen" value="true" />
<param name="movie" value="<?php echo JMONOSLIDESHOW_MEDIA_JS_ADMIN; ?>dewtube.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#000000" />
<param name="flashvars" value="movie=<?php echo JMONOSLIDESHOW_MEDIA_UPLOAD_HTTP.$this->model->filename; ?>&amp;width=<?php echo $info['meta']['onMetaData']['width'] ?>&amp;height=<?php echo $info['meta']['onMetaData']['height'] ?>" />
</object>
        </div>
    <?php endif; ?>
        <div id="dialog-message-thumb">
          <div id="progressbarthumb" ></div>
        </div>
        <input name="upload_etat_thumb" id="upload_etat_thumb" type="hidden" value="" />
        <input name="upload_thumb_name" id="upload_thumb_name" type="hidden" value="" />
        <input name="oldthumbFilename" id="oldthumbFilename" type="hidden" value="<?php echo $this->model->thumbname;?>" /></td>
        <?php endif; ?>
    </tr>
  </table>
  </div>
  <?php if ($this->model->id) : ?>
  <div id="mediaoptions">
    <fieldset class="panelform">
      <ul class="adminformlist">
        <?php if ($this->model->type == 2 ) : ?>
        <?php foreach ($this->form->getFieldset('video') as $field) : ?>
        <?php echo $field->label; ?> <?php echo $field->input; ?>
        <?php endforeach; ?>
        <?php elseif ($this->model->type == 3 ) : ?>
        <?php foreach ($this->form->getFieldset('rss') as $field) : ?>
        <?php echo $field->label; ?> <?php echo $field->input; ?>
        <?php endforeach; ?>
        <?php else : ?>
        <?php foreach ($this->form->getFieldset('image') as $field) : ?>
        <?php echo $field->label; ?> <?php echo $field->input; ?>
        <?php endforeach; ?>
        <?php endif; ?>
      </ul>
    </fieldset>
  </div>
  <?php endif; ?>
  <div id="tagsoptions">
    <fieldset class="panelform">
      <ul class="adminformlist">
        <li>
          <input id='tags' name="tags" type='text' class='tags' value="<?php echo implode(",", $this->model->tags); ?>">
        </li>
      </ul>
    </fieldset>
  </div>
  <div id="rules">
    <fieldset class="panelform">
      <ul class="adminformlist">
        <li><?php echo $this->form->getLabel('rules'); ?> <?php echo $this->form->getInput('rules'); ?></li>
      </ul>
    </fieldset>
  </div>
  </div>
  <div class="clr"></div>
  <input type="hidden" name="task" value="" />
  <input type="hidden" name="mediaid" id="mediaid" value="<?php echo $this->model->id ?>" />
  <?php echo JHTML::_( 'form.token' ); ?>
</form>
